package ci;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.Map.Entry;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.HTMLDocument.Iterator;

import ci.model.RecomendationsCI;
import ci.model.Tester2;

/**
 * Servlet implementation class CiSvt
 */
public class CiSvt extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CiSvt() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	RecomendationsCI sm = new RecomendationsCI();
    	HashMap<String,HashMap> persons = sm.info();
    	
    	String person1 = (String) request.getParameter("cmbPerson1");
    	String person2 = (String) request.getParameter("cmbPerson2");
    	double sim = RecomendationsCI.simDistance(persons, person1, person2);
    	
    	double simPer = RecomendationsCI.simPearson(persons, person1, person2);
    	
    	SortedMap<Double, String> topMatches = Tester2.topMatches(persons, "Toby", 3);
    	
    	request.setAttribute("topMatches", topMatches);
    	
    	request.setAttribute("similitud", sim);
    	request.setAttribute("similitudPer", simPer);

		RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/similitud.jsp");
		requestDispatcher.forward(request, response);

    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
